package com.hxkcloud.nurse.photo;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.hxkcloud.nurse.R;

import java.util.List;

/**
 * 相册列表适配器
 * 
 * @author meeno.tom
 *
 */
public class MeenoPhotoAdapter extends BaseAdapter {
	private Context context;
	private List<MeenoPhotoEntity> list;
	private Button meeno_photo_confirm;
	private LayoutInflater inflater;
	private DisplayImageOptions options;
	private int maxNumber;

	public MeenoPhotoAdapter(Context context, List<MeenoPhotoEntity> list, Button meeno_photo_confirm, int maxNumber) {
		this.maxNumber = maxNumber;
		this.context = context;
		this.list = list;
		this.meeno_photo_confirm = meeno_photo_confirm;
		inflater = LayoutInflater.from(context);
		options = new DisplayImageOptions.Builder().resetViewBeforeLoading(true)
				.showImageOnLoading(R.mipmap.meeno_photo_defeat).showImageForEmptyUri(R.mipmap.meeno_photo_defeat)
				.showImageOnFail(R.mipmap.meeno_photo_defeat).cacheInMemory(true)
				// .cacheOnDisk(true)
				// .displayer(new RoundedBitmapDisplayer(9))
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	public int getSelectedItemCount() {
		int count = 0;
		for (MeenoPhotoEntity entity : list) {
			if (entity.isSelected()) {
				count++;
			}
		}
		return count;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_meeno_photo, null);
			holder = new ViewHolder(convertView);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final int currentPosition = position;
		final ViewHolder currentHolder = holder;
		ImageLoader.getInstance().displayImage("file://" + list.get(currentPosition).getImagePath(),
				holder.item_meeno_photo_icon, options);
		if (list.get(currentPosition).isSelected()) {
			holder.item_meeno_photo_isselected.setImageResource(R.mipmap.meeno_photo_yes);
		} else {
			holder.item_meeno_photo_isselected.setImageResource(R.mipmap.meeno_photo_no);
		}
		holder.img_block.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int selectedCount = getSelectedItemCount();
				if (selectedCount >= maxNumber && !list.get(currentPosition).isSelected()) {
					Toast.makeText(context, "你最多只能选择" + maxNumber + "个图片！", Toast.LENGTH_SHORT).show();
				} else {
					if (list.get(currentPosition).isSelected()) {
						list.get(currentPosition).setSelected(false);
						currentHolder.item_meeno_photo_isselected.setImageResource(R.mipmap.meeno_photo_no);
					} else {
						list.get(currentPosition).setSelected(true);
						currentHolder.item_meeno_photo_isselected.setImageResource(R.mipmap.meeno_photo_yes);
					}
					int currentSelectedCount = getSelectedItemCount();
					if (currentSelectedCount == 0) {
						meeno_photo_confirm.setText("确定");
					} else {
						meeno_photo_confirm.setText(currentSelectedCount + "/" + maxNumber + "  确定");
					}
					// MeenoPhotoAdapter.this.notifyDataSetChanged();
				}
			}
		});
		return convertView;
	}

	class ViewHolder {
		ImageView item_meeno_photo_icon,img_block;
		ImageView item_meeno_photo_isselected;


		public ViewHolder(View view) {

			item_meeno_photo_icon = (ImageView) view.findViewById(R.id.item_meeno_photo_icon);
			item_meeno_photo_isselected = (ImageView) view.findViewById(R.id.item_meeno_photo_isselected);
			img_block = (ImageView) view.findViewById(R.id.img_block);
		}

	}
}
